import * as _ from "lodash"
import { camelCase } from "../case"
import { PageList } from "../data"
import { Author } from "../discuss"
import httpClient from "../httpclient"


export class Review {
  public id: string
  public orderId: string
  public itemId: string
  public productId: string
  public variantId: string
  public author: Author
  public name: string
  public imageUrl: string
  public itemName: string
  public rating: number
  public body: string
  public bodyType: string
  public imageUrls: string[] = []
}

export class ReviewQuery {

  private params = {}

  public page(page?: string | number): ReviewQuery {
    if (page) {
      this.params["page"] = page
    }
    return this
  }

  public limit(limit?: string | number): ReviewQuery {
    if (limit) {
      this.params["limit"] = limit
    }
    return this
  }

  public storeId(storeId: string): ReviewQuery {
    this.params["store_id"] = storeId
    return this
  }

  public customerId(customerId: string): ReviewQuery {
    this.params["customer_id"] = customerId
    return this
  }

  public orderId(orderId: string): ReviewQuery {
    this.params["order_id"] = orderId
    return this
  }

  public productId(productId: string): ReviewQuery {
    this.params["product_id"] = productId
    return this
  }

  public ratings(ratings: number | number[]): ReviewQuery {
    this.params["ratings"] = ratings
    return this
  }

  public list(): Promise<PageList<Review>> {
    return httpClient.get("/v1/products/reviews", {
      params: this.params,
    })
      .then(({ data }) => data)
      .then(camelCase)
      .then(data => {
        return _.assign(new PageList<Review>(), data, {
          elements: _.map(data.elements, review => _.assign(new Review(), review)),
        })
      })
  }

  public count(): Promise<number> {
    return httpClient.get("/v1/products/reviews/count", {
      params: this.params,
    }).then(({ data }) => data)
  }
}

export class ReviewServiceV1 {
  public static createReviewQuery() {
    return new ReviewQuery()
  }
}
